var editingRowIndex;
var isSelectedAll;
var userToolBar;

var columnData;
var frozenColumnData;
$(function() {
	userToolBar = [
			{
				id : "user_select_all",
				text : "",
				plain : true,
				iconCls : "icon-ok",
				handler : function() {
					if (isSelectedAll) {
						$("#user_table").datagrid("unselectAll");
						isSelectedAll = false;
					} else {
						$("#user_table").datagrid("selectAll");
						isSelectedAll = true;
					}
				}
			},
			"-",
			{
				id : "user_add",
				text : "添加",
				plain : true,
				iconCls : "icon-add",
				handler : function() {
					if (editingRowIndex == undefined) {
						$("#user_table").datagrid("insertRow", {
							index : 0,
							row : {}
						});
						$("#user_table").datagrid("addEditor", {
							field : "password",
							editor : {
								type : "validatebox",
								options : {
									required : true,
									validType : "minLength[6]"
								}
							}
						});
						$("#user_table").datagrid("unselectAll");
						editingRowIndex = 0;
						$("#user_table").datagrid("beginEdit", 0);
					}
				}
			},
			{
				id : "user_remove",
				text : "删除",
				plain : true,
				iconCls : "icon-remove",
				handler : function() {
					var selections = $("#user_table").datagrid("getSelections");
					if (selections.length > 0) {
						showConfim("是否执行删除操作？", function(r) {
							if (r) {
								var ids = new Array();
								$.each(selections, function(index, data) {
									ids[index] = data.id;
								});
								var rowIds = "";
								$.each(ids, function(index, value) {
									if (ids.length == (index + 1))
										rowIds += value;
									else
										rowIds += (value + ",");
								});
								$.post("identity/user/removeUsers.html", {
									ids : rowIds
								}, function(data) {
									if (data) {
										showMessage(data.msg);
										if (data.success) {
											$("#user_table").datagrid("load");
										}
									}
								}, "json");

							}
						});
					} else {
						showAlert("请选择要删除的数据！");
					}

				}
			},
			"-",
			{
				id : "user_save",
				text : "保存",
				plain : true,
				iconCls : "icon-save",
				handler : function() {
					if (editingRowIndex == undefined) {
						showAlert("没有需要保存的数据！");
					} else {
						if ($("#user_table").datagrid("validateRow",
								editingRowIndex)) {
							showConfim(
									"是否执行保存操作？",
									function(r) {
										if (r) {
											var rowData = $("#user_table")
													.datagrid("getRows")[editingRowIndex];
											$("#user_table").datagrid(
													"endEdit", editingRowIndex);
											editingRowIndex = undefined;
											$
													.post(
															"identity/user/saveUser.html",
															rowData,
															function(data) {
																if (data) {
																	showMessage(data.msg);
																	if (data.success) {
																		$(
																				"#user_table")
																				.datagrid(
																						"reload");
																	}
																}
															}, "json");

										}
									});
						}
					}

				}
			}, {
				id : "user_undo",
				text : "取消",
				plain : true,
				iconCls : "icon-undo",
				handler : function() {
					if (editingRowIndex != undefined) {
						$("#user_table").datagrid("rejectChanges");
						editingRowIndex = undefined;
					}
					$("#user_table").datagrid("unselectAll");
					isSelectedAll = false;
				}
			} ];
	frozenColumnData = [ [ {
		field : "account",
		title : "账号",
		width : 100,
		sortable : true,
		editor : {
			type : "validatebox",
			options : {
				 validType :"betweenLength[8,16]",
				required : true
			}
		}
	}, {
		field : "email",
		title : "电子邮件",
		width : 150,
		sortable : true,
		editor : {
			type : "validatebox",
			options : {
				 validType :"email",
				required : true
			}
		}
	} ] ];

	columnData = [ [ {
		field : "password",
		title : "密码",
		width : 100,
		formatter : function(value, rowData, rowIndex) {
			return "******";
		}
	}, {
		field : "gender",
		title : "性别",
		width : 50,
		formatter : function(value, row, index) {
			if (value == "1")
				return "男";
			else
				return "女";
		},
		editor : {
			type : "combobox",
			options : {
				panelHeight : 45,
				required : true,
				editable : false,
				data : [ {
					text : "男",
					value : "1"
				}, {
					text : "女",
					value : "2"
				} ]
			}
		}
	}, {
		field : "birthday",
		title : "出生日期",
		width : 100,
		sortable : true,
		editor : {
			type : "datebox",
			options : {
				required : true,
				editable : false
			}
		}
	}, {
		field : "height",
		title : "身高(CM)",
		width : 70,
		sortable : true,
		editor : {
			type : "numberbox",
			options : {
				validType : "betweenNumber[100,250]"
			}
		}
	}, {
		field : "weight",
		title : "体重(KG)",
		width : 70,
		sortable : true,
		editor : {
			type : "numberbox",
			options : {
				validType : "betweenNumber[30,150]"
			}
		}
	}, {
		field : "hobby",
		title : "爱好",
		width : 100
	}, {
		field : "createTime",
		title : "创建时间",
		sortable : true,
		width : 150
	}, {
		field : "createIP",
		title : "创建IP地址",
		width : 100
	}, {
		field : "createUserId",
		title : "创建用户",
		width : 100
	}, {
		field : "state",
		title : "状态",
		width : 50,
		sortable : true,
		formatter : function(value, row, index) {
			if (value == "1")
				return "激活";
			else
				return "禁用";
		},
		editor : {
			type : "combobox",
			options : {
				panelHeight : 45,
				required : true,
				editable : false,
				data : [ {
					text : "激活",
					value : "1"
				}, {
					text : "禁用",
					value : "0"
				} ]
			}
		}
	} ] ];

	$("#user_table").datagrid({
		fit : true,
		toolbar : userToolBar,
		idField : "id",
		rownumbers : true,
		url : "identity/user/listUsers.html",
		pagination : true,
		pageSize : 10,
		pageList : [ 10, 20, 50, 100, 200, 500 ],
		sortName : "id",
		sortOrder : "desc",
		nowrap : false,
		onDblClickRow : function(inx) {
			if (editingRowIndex == undefined) {
				$("#user_table").datagrid("removeEditor", "password");
				$("#user_table").datagrid("unselectAll");
				editingRowIndex = inx;
				$("#user_table").datagrid("beginEdit", inx);
			}
		},
		onSelect : function(inx) {
			if (editingRowIndex != undefined) {
				$("#user_table").datagrid("unselectRow", inx);
			}
		},
		columns : columnData,
		frozenColumns : frozenColumnData
	});
	$(".datagrid-header div").css("textAlign", "center");
});